Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • | for optional arguments

    Hi, I'm starting to program a class, and I am having trouble when defining the setup() function for the class with an optional argument. The code in my .mata is

    Code:
    version 15
    mata:
    class nelogit
    {
        // Definition of variables that are common to all estimators, thus
        // making them protected so they can inherit them.
        protected:
            real matrix X, S                                                        // explanatory variables and scale variables
            real colvector b, bS, g                                                    // coefficients, scaled coefficients, and gradient
            real matrix V, H                                                        // Variance and Hessian
            real colvector y                                                        // vector of the dependent variable
            real scalar j                                                            // number of alternatives
            real scalar ll0, ll                                                        // log-likelihoods of constants-only and estimated models
            real scalar r2                                                            // McFadden's pseudo-rsquared
        
        public void setup()
        
        // Functions implemented in this class
        private:
            real scalar ll0()                                                        // Returns the ll of constants-only model
            real scalar r2()                                                        // Returns McFadden's pseudo R-squared
    }
    // Setup functions for the class
    void nelogit::setup(real colvector user_y,
        real matrix user_X,
        real scalar user_j |,
        real matrix user_S)
    {
        this.y = user_y
        this.X = user_X
        this.j = user_j
        if (args()>3)
            this.S = user_S
        this.ll0 = this.ll0()
    }
    ......
    
    end
    where the dots indicate other functions, none of which have | anywhere in them. When I "do" this file I get
    Code:
    . quiet do "`pdir'/nelogit.mata"
    '|' found where ')' expected
    (41 lines skipped)
    r(3000);
    
    end of do-file
    
    r(3000);
    As soon as I take the vertical line out it runs file of course, but the point is that I want the matrix user_S to be optional. If it's there we would be modeling scale heterogeneity if not we wouldn't.
    Last edited by Alfonso Sánchez-Peñalver; 25 Aug 2020, 15:53.
    Alfonso Sanchez-Penalver

  • #2
    Put the pipe after the comma and on the next line.
    Code:
        real scalar user_j,
        | real matrix user_S)

    Comment


    • #3
      Thanks Joseph Coveney !!!! I guess I should read the errata on Gould's book!!! Just saw that. It was driving me nuts.
      Alfonso Sanchez-Penalver

      Comment

      Working...
      X